package com.avocado.configuration.service.impl;


import com.avocado.base.entity.User;
import com.avocado.base.service.UserService;
import com.avocado.configuration.dto.DataDictionaryAddDto;
import com.avocado.configuration.dto.DataDictionaryEditDto;
import com.avocado.configuration.entity.DataDictionary;
import com.avocado.configuration.repository.DataDictionaryRepository;
import com.avocado.configuration.service.DataDictionaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class DataDictionaryServiceImpl implements DataDictionaryService {

    private final DataDictionaryRepository dataDictionaryRepository;

    private final UserService userService;

    public DataDictionaryServiceImpl(DataDictionaryRepository dataDictionaryRepository, UserService userService) {
        this.dataDictionaryRepository = dataDictionaryRepository;
        this.userService = userService;
    }

    @CacheEvict(value="dataDictionary",key="#dto.key")
    @Override
    public Long add(DataDictionaryAddDto dto, String username) {

        User loginUser=userService.findByName(username);

        DataDictionary dataDictionary=new DataDictionary();

        BeanUtils.copyProperties(dto,dataDictionary);

        dataDictionary.setCreatedById(loginUser.getId());

        dataDictionary.setCreateTime(new Date());

        dataDictionaryRepository.saveAndFlush(dataDictionary);

        return dataDictionary.getId();
    }


    @CacheEvict(value="dataDictionary",key="#dto.key")
    @Override
    public void update(DataDictionaryEditDto dto, String username) {

        Optional<DataDictionary> optional=dataDictionaryRepository.findById(dto.getId());

        DataDictionary dataDictionary =optional.get();

        BeanUtils.copyProperties(dto,dataDictionary);

        dataDictionaryRepository.saveAndFlush(dataDictionary);

    }

    @CacheEvict(value="dataDictionary",key="#key")
    @Override
    public Boolean deleteById(Long id,String key) {

        long totalCount=dataDictionaryRepository.getCountByParentId(id);

        if(totalCount>0L) {

            return Boolean.FALSE;
        }

        Optional<DataDictionary> optional=dataDictionaryRepository.findById(id);

        DataDictionary dataDictionary=optional.get();

        dataDictionaryRepository.delete(dataDictionary);

        return Boolean.TRUE;

    }

    @Override
    public List<DataDictionary> findAll() {

        return dataDictionaryRepository.findAll();

    }


    @Cacheable(value="dataDictionary",key="#key")
    public String getValueByKey(String key){

        List<DataDictionary> dataDictionaryList=dataDictionaryRepository.findAllByKey(key);

        if(!dataDictionaryList.isEmpty()){
            return dataDictionaryList.get(0).getValue();
        }
        return null;
    }
}
